<template>
  <div style="display: flex;justify-content: center;">
    <el-button
        size="mini"
        :type="item.type"
        v-for="(item,index) in columnEventListPermisson"
        :key="index"
        @click="actionHandler(item,scope)"
        :disabled="disabledColumn(item,scope)"
        :icon="item.icon"
        v-show="hideColumn(item,scope)">{{item.text}}
    </el-button>
  </div>
</template>

<script>
export default {
  name: "crud-plus-btn",
  props: ['columnEventList', 'formCode', 'scope'],
  computed: {
    columnEventListPermisson () {
      let temp = []
      for (let i = 0; i < this.columnEventList.length; i++) {
        const element = this.columnEventList[i]
        if (this.$hasPermission(this.formCode + ':' + element.action)) {
          // temp.push(element)
        }
      }
      temp = this.columnEventList
      return temp
    }
  },
  methods: {
    actionHandler (e, row) {
      this.$emit('actionHandler', e, row)
    },
    disabledColumn (item, row) {
      let temp = false
      if(Array.isArray(item.condition)&&item.condition.length > 0) {
        temp =  item.condition.some(i=> this.appendFuncList(i, row))
      }else{
        if (item.condition && item.condition.column) {
          if (item.condition.action === 'disable') {
            temp = this.appendFunc(item, row)
          }
        }
      }
      return temp
    },
    hideColumn (item, row) {
      let temp = true
      if (item.condition && item.condition.column) {
        if (item.condition.action === 'hide') {
          temp = !this.appendFunc(item, row)
        }
      }
      return temp
    },
    appendFunc (item, row) {
      // !==
      if (item.condition.opt === '!==') {
        if (row[item.condition.column] !== item.condition.value) {
          return true
        }
      }
      // ===
      if (item.condition.opt === '===') {
        if (row[item.condition.column] === item.condition.value) {
          return true
        }
      }
      // in & List value
      if (item.condition.opt === 'in' && Array.isArray(item.condition.value)) {
        let temp = false
        for (let i = 0; i < item.condition.value.length; i++) {
          const element = item.condition.value[i]
          if (row[item.condition.column] === element) {
            temp = true
            break
          }
        }
        return temp
      }
      // not null
      if (item.condition.opt === 'notnull') {
        if (row && row[item.condition.column]) {
          return true
        }
      }
    },
    appendFuncList (item, row) {
      // !==
      if (item.opt === '!==') {
        if (row[item.column] !== item.value) {
          return true
        }
      }
      // ===
      if (item.opt === '===') {
        if (row[item.column] === item.value) {
          return true
        }
      }
      // in & List value
      if (item.opt === 'in' && Array.isArray(item.value)) {
        let temp = false
        for (let i = 0; i < item.value.length; i++) {
          const element = item.value[i]
          if (row[item.column] === element) {
            temp = true
            break
          }
        }
        return temp
      }
      // not null
      if (item.opt === 'notnull') {
        if (row && row[item.column]) {
          return true
        }
      }
    }
  }
}
</script>

<style scoped>

</style>